using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BagView : View
{
    
    string bag = "Bag/Scroll View/Viewport/Content/Prop_";

    string equip = "Roles/";

    Transform content_parent = null;
    Transform Roles =null;

    Transform PropInspector;
    Transform MaterialInspector;
    Transform EquipInspector;

    Text PropName, PropInfo, PropAct;

    Transform select;
    Transform cmd;

    BagEntity selectItem;
    
    public override void Awake()
    {
        base.Awake();
        content_parent = transform.Find("Bag/Scroll View/Viewport/Content");
        GetComponent<Button>("Bag/Menu/All").onClick.AddListener(() => { Show(); });
        GetComponent<Button>("Bag/Menu/Prop").onClick.AddListener(() => { Show(0); });
        GetComponent<Button>("Bag/Menu/Equip").onClick.AddListener(() => { Show(1); });
        GetComponent<Button>("Bag/Menu/Material").onClick.AddListener(() => { Show(2); });

        GetComponent<Button>("Bag/Sort").onClick.AddListener(() => { BagData.Instance.SortByType(); });

        PropInspector = transform.Find("Prop");
        MaterialInspector = transform.Find("Material");
        EquipInspector = transform.Find("Equip");


        select = GetComponent<Transform>("Bag/Select");
        Roles = transform.Find("Roles");
        for (int i = 0; i < content_parent.childCount; i++)
        {
            var btn = content_parent.GetChild(i).GetComponent<Button>();
            btn.onClick.AddListener(() =>{
                if(btn.transform.childCount > 0)
                {
                    select.gameObject.SetActive(true);
                    select.position = GameSystem.Instance.eventSystem.currentSelectedGameObject.transform.position;
                    int id = i;
                    selectItem = BagData.Instance.Get(id);
                }
                else
                {
                    select.gameObject.SetActive(false);
                }

            });
        }

        //for(int i = 1;i< Roles.childCount; i++)
        //{

        //    这里要解决下 隐藏Select的问题
        //    var btn = content_parent.GetChild(i).GetComponentInChildren<Button>();

        //    if (btn != null)
        //    {
        //        btn.onClick.AddListener(() => {
        //            select.gameObject.SetActive(false);
        //        });
        //    }

        //}

        cmd = transform.Find("CMD");

        cmd.Find("Use").GetComponent<Button>().onClick.AddListener(() =>
        {
            
        });

        cmd.Find("Discard").GetComponent<Button>().onClick.AddListener(() =>
        {
            BagData.Instance.Remove_By_Grild_Id(selectItem.grild_id, selectItem.num);
            Show(-1);
            selectItem = null;
            cmd.gameObject.SetActive(false);
        });


        
    }

    public override void OnEnable()
    {
        base.OnEnable();

        
        //清理上一次的残留
        //Show(-1);
    }

    //BagView
    Dictionary<int,GameObject> bag_item =new Dictionary<int,GameObject>();

    Dictionary<int, GameObject> equip_item = new Dictionary<int, GameObject>();
    public GameObject SelectObj { get; internal set; }

    public void GetMouseButtonDown_1()
    {
        if (UInput.GetMouseButtonDown_1())
        {

            if (SelectObj != null)
            {
                Transform item = GameSystem.Instance.GetCurSelectItem(SelectObj,"Prop_");
                if (item == null) return;
                cmd.gameObject.SetActive(true);
                select.gameObject.SetActive(true);
                cmd.position = item.position;
                select.position = item.position; 
                int id = int.Parse(item.gameObject.name.Split("_")[1]);
                selectItem = BagData.Instance.Get(id);
            }
            else
            {
                cmd.gameObject.SetActive(false);
                select.gameObject.SetActive(false);
            }
        }
    }
    public void Show(int type = -1)
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
        foreach (var item in bag_item)
        {
            //清理上次的
            ResourceManager.Instance.Destory_BagItem(item.Value);
        }
        bag_item.Clear();

        foreach (var item in equip_item)
        {
            //清理上次的
            ResourceManager.Instance.Destory_BagItem(item.Value);
        }
        equip_item.Clear();

        var data = BagData.Instance.dct;
        foreach (var item in data)
        {
            var obj = CreateItem(item.Value);
            var parent = $"{bag}{item.Key}";
            var p = transform.Find(parent);
            obj.transform.SetParent(p, false);
            bag_item[item.Value.grild_id] = obj;
        }

        var equDct = BagData.Instance.equip_dct;
        foreach (var item in equDct)
        {
            var obj = CreateItem(item.Value);

            string part =  item.Key switch
            {
                1 => "Helmet/",
                2 => "Cloth/",
                3 => "Capes/",
                4 => "Pants/",
                5 => "Boots/",
                6 => "Weapon/",
                7 => "Earrings/",
                8 => "Necklaces/",
                9 => "Rings/",
                10=> "Belt/",
                _=> "Helmet/",
            };
            var parent = $"{equip}{part}Item_{item.Key}";
            var p = transform.Find(parent);
            obj.transform.SetParent(p, false);
            equip_item[item.Value.grild_id] = obj;
        }

        if (type == -1) //全部
        {
            for(int i= 0; i < content_parent.childCount; i++)
            {
                content_parent.GetChild(i).gameObject.SetActive(true);
            }
        }
        else 
        {
            for (int i = 0; i < content_parent.childCount; i++)
            {
                var obj = content_parent.GetChild(i);
                var grildInfo = BagData.Instance.Get(i);
                if (grildInfo != null)
                {
                    obj.gameObject.SetActive((grildInfo.propConfig.type == type));
                }
                else
                {
                    obj.gameObject.SetActive(false);
                }
            }
        }
    }


    public GameObject CreateItem(BagEntity bagEntity)
    {
        var obj = ResourceManager.Instance.CreatePropItem(bagEntity.id);

        //var parent = $"{content}{bagEntity.propConfig}";
        //var p = transform.Find(parent);
        //obj.transform.SetParent(p, false);

        return obj;

    }


    public void Follow(GameObject obj,Vector3 point)
    {
        obj.transform.position = point; 
    }




    public void SetPropInfo(Vector3 pos, int grild_Id =-1 ,bool isShow = true)
    {
        BagEntity bagEntity = BagData.Instance.Get(grild_Id);
        if (bagEntity != null)
        {
            //物品名称 作用描述

            if (PropName == null)
            {
                PropName = transform.Find("Prop/Name").GetComponent<Text>();
                PropInfo = transform.Find("Prop/Info").GetComponent<Text>();
                PropAct = transform.Find("Prop/Act").GetComponent<Text>();
            }
            PropName.text = bagEntity.propConfig.name;
            PropInfo.text= bagEntity.propConfig.info;
            //这里看一下GUI有没有类似FGUI的富文本的 符号表达 作输入
            string level = bagEntity.propConfig.level == 0 ? "无限制" : bagEntity.propConfig.level.ToString() + "级";
            PropAct.text = $"类型:物品\n使用等级:{level}";
            var scale = transform.localScale.x / 1;

        }
        PropInspector.position = pos;
        PropInspector.gameObject.SetActive(isShow);
    }
    public void SetMaterialInfo(bool isShow = true)
    {
        MaterialInspector.gameObject.SetActive(isShow);
    }
    public void SetEquipInfo(bool isShow = true)
    {
        EquipInspector.gameObject.SetActive(isShow);
    }


    public override void Update()
    {
        base.Update();
        GetMouseButtonDown_1();
    }
}
